// pages/index/index.js
import { request } from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图数据
    bannerlist: [],
    // 推荐歌单数据
    recommendlist: [],
    //猜你喜欢
    likeList: [],
    //歌单数据
    songList: [],
  },
  // 跳转到每日推荐页面
  torecommendSong() {
    wx.navigateTo({
      url: "/pages/recommendSong/recommendSong",
    });
  },
  //跳转到歌单列表
  toSongList() {
    wx.navigateTo({
      url: "/pages/songList/songList",
    });
  },

  toClassification() {
    wx.switchTab({
      url: "/pages/classification/classification",
    });
  },

  toCollect() {
    wx.navigateTo({
      url: "/pages/collect/collect",
    });
  },

  toFun() {
    wx.switchTab({
      url: "/pages/fun/fun",
    });
  },

  // 跳转至搜索页面
  toSearch() {
    wx.navigateTo({
      url: "/pages/search/search",
    });
  },
  //跳转歌单详情
  tosongs(e) {
    let index = e.currentTarget.dataset.index;
    let currentMusic = this.data.songList[index];
    wx.navigateTo({
      url: "/pages/songs/songs",
      success: function (res) {
        res.eventChannel.emit("detail", { musicData: currentMusic });
      },
    });
  },
  recommendPaly(e) {
    let index = e.currentTarget.dataset.index;
    let currentMusic = this.data.recommendlist[index];
    wx.navigateTo({
      url: "/pages/songDetail/songDetail",
      success: function (res) {
        res.eventChannel.emit("detail", { musicData: currentMusic });
      },
    });
  },
  likePaly(e) {
    let inner = e.currentTarget.dataset.inner;
    let out = e.currentTarget.dataset.out;
    let currentMusic = this.data.likeList[inner][out];
    wx.navigateTo({
      url: "/pages/songDetail/songDetail",
      success: function (res) {
        res.eventChannel.emit("detail", { musicData: currentMusic });
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function () {
    // 获取轮播图数据
    let bannerListData = await request("/swiper");
    this.setData({
      bannerlist: bannerListData.data,
    });
    //  获取推荐歌曲数据
    let recommendListData = await request("/music/condition", "GET", {
      isTop: 1,
    });
    let recommendlist = recommendListData.data.filter((item, index) => {
      return index <= 9;
    });
    this.setData({
      recommendlist,
    });
    //精选歌单
    let songListData = await request("/song");
    let songList = songListData.data.filter((item, index) => {
      return index <= 9;
    });
    this.setData({
      songList,
    });
    // 获取从猜你喜欢数据
    let likeListData = await request("/music/condition", "GET", { isLike: 1 });
    let length = likeListData.data.length;
    let count = Math.floor(length / 3);
    let newList = [];
    for (let i = 1; i <= count; i++) {
      let temp = likeListData.data.filter((item, index) => {
        return index < 3 * i && index > 3 * i - 4;
      });
      newList.push(temp);
    }
    this.setData({
      likeList: newList,
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function ({ from }) {
    console.log(from);
  },
});
